package xyz.wanwanwan.www.hrm.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.wanwanwan.www.hrm.constant.CoursePromotionState;
import xyz.wanwanwan.www.hrm.constant.PayCoursePromotionConstant;
import xyz.wanwanwan.www.hrm.domain.CoursePromotion;
import xyz.wanwanwan.www.hrm.domain.OrderCoursePromotion;
import xyz.wanwanwan.www.hrm.domain.OrderCoursePromotionDetail;
import xyz.wanwanwan.www.hrm.feign.client.CacheFeign;
import xyz.wanwanwan.www.hrm.mapper.CoursePromotionMapper;
import xyz.wanwanwan.www.hrm.mapper.OrderCoursePromotionDetailMapper;
import xyz.wanwanwan.www.hrm.mapper.OrderCoursePromotionMapper;
import xyz.wanwanwan.www.hrm.service.IOrderCoursePromotionService;
import xyz.wanwanwan.www.hrm.util.AjaxResult;
import xyz.wanwanwan.www.hrm.util.SerializeUtil;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Fu
 * @since 2020-09-18
 */
@Service
public class OrderCoursePromotionServiceImpl extends ServiceImpl<OrderCoursePromotionMapper, OrderCoursePromotion> implements IOrderCoursePromotionService {
    @Autowired
    private OrderCoursePromotionMapper orderCoursePromotionMapper;
    @Autowired
    private CacheFeign cacheFeign;
    @Autowired
    private OrderCoursePromotionDetailMapper orderCoursePromotionDetailMapper;
    @Autowired
    private CoursePromotionMapper coursePromotionMapper;
    @Override
    public void buyCoursePromotion(Long id) {
        Date now = new Date();
        //获取促销课程
        byte[] buyCoursePromotion = cacheFeign.getBuyCoursePromotion(id);
            //String string = JSON.toJSONString(buyCoursePromotion.getResultObj());
            //CoursePromotion value = JSON.parseObject(string, CoursePromotion.class);
            //JSONObject jsonObject= JSONObject.fromObject(buyCoursePromotion.getResultObj());
            //将json转成需要的对象
            //CoursePromotion value = (CoursePromotion)JSONObject.toBean(jsonObject, CoursePromotion.class);
            //CoursePromotion value = (CoursePromotion)buyCoursePromotion.getResultObj();
            //1．抢购时间是否在有效时间范围
            //2．抢购的课程状态是否是抢购中
            //3．抢购的课程的剩余数量是否 > 0
        String s = JSON.toJSONString(SerializeUtil.unserialize(buyCoursePromotion));
        CoursePromotion value = JSON.parseObject(s, CoursePromotion.class);
        if(value!=null){
        if(((now.getTime()>value.getStartTime().getTime())&&(now.getTime()<value.getEndTime().getTime()))
                    &&value.getState().equals(CoursePromotionState.COURSE_PROMOTION_STATUS_LOADING)
                    &&value.getNumber()>0){
                //库存量减少后保存
                value.setNumber(value.getNumber()-1);
                AjaxResult ajaxResult = cacheFeign.savePromotion(value);
                if(ajaxResult.isSuccess()){
                    //保存促销数量
                    coursePromotionMapper.updateById(value);
                    OrderCoursePromotion orderCoursePromotion = new OrderCoursePromotion();
                    orderCoursePromotion.setCoursePromotionId(value.getId());
                    orderCoursePromotion.setPayPrice(value.getPrice());
                    orderCoursePromotion.setTotalNumber(1);
                    orderCoursePromotion.setUserId(2L);
                    orderCoursePromotion.setId(value.getId());
                    ////2. 订单状态为“待支付”
                    orderCoursePromotion.setState(PayCoursePromotionConstant.COURSE_PROMOTION_NO_PAY);
                    orderCoursePromotion.setPayApplyTime(now);
                    orderCoursePromotion.setPayLastTime(new Date(now.getTime()+10*6000));
                    //保存订单
                    orderCoursePromotionMapper.insert(orderCoursePromotion);
                    //订单详情
                    OrderCoursePromotionDetail orderCoursePromotionDetail = new OrderCoursePromotionDetail();
                    orderCoursePromotionDetail.setCoursePromotionId(value.getId());
                    orderCoursePromotionDetail.setOrderCoursePromotionId(orderCoursePromotion.getId());
                    orderCoursePromotionDetail.setNumber(1);
                    orderCoursePromotionDetail.setPrice(value.getPrice());
                    //保存订单详情
                    orderCoursePromotionDetailMapper.insert(orderCoursePromotionDetail);
                }else{
                    throw new RuntimeException("网络错误");
                }
            }
        }
    }
}
